<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm add" v-show="active">
      <el-form-item label="博客名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item label="博客分类" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择博客分类">
          <el-option label="娱乐" value="yule"></el-option>
          <el-option label="体育" value="tiyu"></el-option>
          <el-option label="文学" value="wenxue"></el-option>
          <el-option label="IT" value="it"></el-option>
          <!--<el-option v-for="item in types" :label="item.title" :value="item.value"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item label="发表时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker type="fixed-time" placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="公开" prop="public">
        <el-switch v-model="ruleForm.public"></el-switch>
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-checkbox-group v-model="ruleForm.tag">
          <el-checkbox v-for="item in tags" :label="item" name="tag"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="允许评论" prop="comment">
        <el-radio-group v-model="ruleForm.comment">
          <el-radio label="允许"></el-radio>
          <el-radio label="禁止"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介" prop="desc">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <div v-show="!active" class="add">
      <p>{{msg}}</p>
      <el-button @click="active = !active" type="info">继续添加</el-button>
    </div>
  </div>

</template>
<script>
  import axios from 'axios'

  export default {
    data() {
      return {
        active: true,
        msg: '',
        tags: ['活动', '主题', '品牌', '美食'],
        // types: [
        //   {title: '娱乐', vaule: 'yule'},
        //   {title: '体育', vaule: 'tiyu'},
        //   {title: '文学', vaule: 'wenxue'},
        //   {title: 'IT', vaule: 'it'},
        //   {title: '历史', vaule: 'lishi'},
        // ],
        ruleForm: {
          name: '',
          author: '',
          type: '',
          date1: '',
          date2: '',
          public: false,
          tag: [],
          comment: '',
          desc: ''
        },
        rules: {
          name: [
            {required: true, message: '请输入博客名称', trigger: 'blur'},
            {min: 3, max: 5, message: '长度在 3 到 25 个字符', trigger: 'blur'}
          ],
          author: [
            {required: true, message: '请输入作者名称', trigger: 'blur'},
            {min: 3, max: 5, message: '长度在 3 到 15 个字符', trigger: 'blur'}
          ],
          type: [
            {required: true, message: '请选择分类', trigger: 'change'}
          ],
          date1: [
            {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
          ],
          date2: [
            {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
          ],
          tag: [
            {type: 'array', required: true, message: '请至少选择一个标签', trigger: 'change'}
          ],
          comment: [
            {required: true, message: '请选择是否评论', trigger: 'change'}
          ],
          desc: [
            {required: true, message: '请填写简介', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            /*fetch('https://wd7995297966yjuctj.wilddogio.com/blogs.json',{
                method: "POST",
                mode: "no-cors",// 解决跨域
                headers: {formName
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(this.ruleForm)
            }).then( res => {
                this.$router.push('/list')
            })*/
            axios.post('https://wd7995297966yjuctj.wilddogio.com/blogs.json', this.ruleForm).then(res => {
              // this.$router.push('/list')
              this.active = false;
              this.msg = '博客发表成功!!!'
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>
  .add {
    width: 600px;
    margin: 0 auto;
    padding-top: 50px;
  }

  .el-col-2 {
    text-align: center;
  }
</style>
